//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MTAG_EIC.h"
#include "uListeDepartements.h"

#define MODULE_TAG MTAG_ULISTEDEPARTEMENTS

//---------------------------------------------------------------------------

#pragma package(smart_init)

__fastcall TListeDepartements::TListeDepartements()
{
  ASSERT(DEP_DERNIER == 105);

  struct STRUCT
  {
    char* szNomDep;
    char* szCodeDep;
    int   iID;
  };

//--------------------------------------------------------------------------------
//                        INITIALISATION DE "TabDepartements[]"
//--------------------------------------------------------------------------------
STRUCT TabDepartements[] = { {"AIN"                     , "01" ,AIN},
                             {"AISNE"                   , "02" ,AISNE},
                             {"ALLIER"                  , "03" ,ALLIER},
                             {"ALPES DE HAUTE PROVENCE" , "04" ,ALPES_DE_HAUTE_PROVENCE},
                             {"HAUTES ALPES"            , "05" ,HAUTES_ALPES},
                             {"ALPES MARITIMES"         , "06" ,ALPES_MARITIMES},
                             {"ARDECHE"                 , "07" ,ARDECHE},
                             {"ARDENNES"                , "08" ,ARDENNES},
                             {"ARIEGE"                  , "09" ,ARIEGE},
                             {"AUBE"                    , "10" ,AUBE},
                             {"AUDE"                    , "11" ,AUDE},
                             {"AVEYRON"                 , "12" ,AVEYRON},
                             {"BOUCHES DU RHONE"        , "13" ,BOUCHES_DU_RHONE},
                             {"CALVADOS"                , "14" ,CALVADOS},
                             {"CANTAL"                  , "15" ,CANTAL},
                             {"CHARENTE"                , "16" ,CHARENTE},
                             {"CHARENTE MARITIME"       , "17" ,CHARENTE_MARITIME},
                             {"CHER"                    , "18" ,CHER},
                             {"CORREZE"                 , "19" ,CORREZE},
                             {"CORSE DU SUD"            , "20A",CORSE_DU_SUD},
                             {"HAUTE CORSE"             , "20B",HAUTE_CORSE},
                             {"COTE D'OR"               , "21" ,COTE_D_OR},
                             {"COTES D'ARMOR"           , "22" ,COTE_D_ARMOR},
                             {"CREUSE"                  , "23" ,CREUSE},
                             {"DORDOGNE"                , "24" ,DORDOGNE},
                             {"DOUBS"                   , "25" ,DOUBS},
                             {"DROME"                   , "26" ,DROME},
                             {"EURE"                    , "27" ,EURE},
                             {"EURE ET LOIR"            , "28" ,EURE_ET_LOIR},
                             {"FINISTERE"               , "29" ,FINISTERE},
                             {"GARD"                    , "30" ,GARD},
                             {"HAUTE GARONNE"           , "31" ,HAUTE_GARONNE},
                             {"GERS"                    , "32" ,GERS},
                             {"GIRONDE"                 , "33" ,GIRONDE},
                             {"HERAULT"                 , "34" ,HERAULT},
                             {"ILLE ET VILAINE"         , "35" ,ILLE_ET_VILAINE},
                             {"INDRE"                   , "36" ,INDRE},
                             {"INDRE ET LOIRE"          , "37" ,INDRE_ET_LOIR},
                             {"ISERE"                   , "38" ,ISERE},
                             {"JURA"                    , "39" ,JURA},
                             {"LANDES"                  , "40" ,LANDES},
                             {"LOIR ET CHER"            , "41" ,LOIR_ET_CHER},
                             {"LOIRE"                   , "42" ,LOIRE},
                             {"HAUTE LOIRE"             , "43" ,HAUTE_LOIRE},
                             {"LOIRE ATLANTIQUE"        , "44" ,LOIRE_ATLANTIQUE},
                             {"LOIRET"                  , "45" ,LOIRET},
                             {"LOT"                     , "46" ,LOT},
                             {"LOT ET GARONNE"          , "47" ,LOT_ET_GARONNE},
                             {"LOZERE"                  , "48" ,LOZERE},
                             {"MAINE ET LOIRE"          , "49" ,MAINE_ET_LOIRE},
                             {"MANCHE"                  , "50" ,MANCHE},
                             {"MARNE"                   , "51" ,MARNE},
                             {"HAUTE MARNE"             , "52" ,HAUTE_MARNE},
                             {"MAYENNE"                 , "53" ,MAYENNE},
                             {"MEURTHE ET MOSELLE"      , "54" ,MEURTHE_ET_MOSELLE},
                             {"MEUSE"                   , "55" ,MEUSE},
                             {"MORBIHAN"                , "56" ,MORBIHAN},
                             {"MOSELLE"                 , "57" ,MOSELLE},
                             {"NIEVRE"                  , "58" ,NIEVRE},
                             {"NORD"                    , "59" ,NORD},
                             {"OISE"                    , "60" ,OISE},
                             {"ORNE"                    , "61" ,ORNE},
                             {"PAS DE CALAIS"           , "62" ,PAS_DE_CALAIS},
                             {"PUY DE DOME"             , "63" ,PUY_DE_DOME},
                             {"PYRENEES ATLANTIQUES"    , "64" ,PYRENEES_ATLANTIQUES},
                             {"HAUTES PYRENEES"         , "65" ,HAUTES_PYRENEES},
                             {"PYRENEES ORIENTALES"     , "66" ,PYRENEES_ORIENTALES},
                             {"BAS RHIN"                , "67" ,BAS_RHIN},
                             {"HAUT RHIN"               , "68" ,HAUT_RHIN},
                             {"RHONE"                   , "69" ,RHONE},
                             {"HAUTE SAONE"             , "70" ,HAUTE_SOANE},
                             {"SAONE ET LOIRE"          , "71" ,SOANE_ET_LOIRE},
                             {"SARTHE"                  , "72" ,SARTHE},
                             {"SAVOIE"                  , "73" ,SAVOIE},
                             {"HAUTE SAVOIE"            , "74" ,HAUTE_SAVOIE},
                             {"PARIS"                   , "75" ,PARIS},
                             {"SEINE MARITIME"          , "76" ,SEINE_MARITIME},
                             {"SEINE ET MARNE"          , "77" ,SEINE_ET_MARNE},
                             {"YVELINES"                , "78" ,YVELINES},
                             {"DEUX SEVRES"             , "79" ,DEUX_SEVRES},
                             {"SOMME"                   , "80" ,SOMME},
                             {"TARN"                    , "81" ,TARN},
                             {"TARN ET GARONNE"         , "82" ,TARN_ET_GARONNE},
                             {"VAR"                     , "83" ,VAR},
                             {"VAUCLUSE"                , "84" ,VAUCLUSE},
                             {"VENDEE"                  , "85" ,VENDEE},
                             {"VIENNE"                  , "86" ,VIENNE},
                             {"HAUTE VIENNE"            , "87" ,HAUTE_VIENNE},
                             {"VOSGES"                  , "88" ,VOSGES},
                             {"YONNE"                   , "89" ,YONNE},
                             {"TERRITOIRE DE BELFORT"   , "90" ,TERRITOIRE_DE_BELFORT},
                             {"ESSONE"                  , "91" ,GUINEE_BISSAU},
                             {"HAUTS DE SEINE"          , "92" ,ESSONE},
                             {"SEINE SAINT DENIS"       , "93" ,HAUTS_DE_SEINE},
                             {"VAL DE MARNE"            , "94" ,VAL_DE_MARNE},
                             {"VAL D'OISE"              , "95" ,VAL_D_OISE},
                             {"GUADELOUPE"              ,"971" ,GUADELOUPE},
                             {"MARTINIQUE"              ,"972" ,MARTINIQUE},
                             {"GUYANE"                  ,"973" ,GUYANE},
                             {"REUNION"                 ,"974" ,REUNION},
                             {"SAINT PIERRE ET MIQUELON","975" ,SAINT_PIERRE_ET_MIQUELON},
                             {"MAYOTTE"                 ,"976" ,MAYOTTE},
                             {"WALLIS ET FUTUNA"        ,"986" ,WALLIS_ET_FUTUNA},
                             {"POLYNESIE FRANCAISE"     ,"987" ,POLYNESIE_FRANCAISE},
                             {"NOUVELLE CALEDONIE"      ,"988" ,NOUVELLE_CALEDONIE},
                             {""                        ,   "" ,DEP_DERNIER}};

  try
  {
    slDepartements = new TStringList;
    for (int i=0 ; TabDepartements[i].szNomDep[0] != '\0'; i++)
      slDepartements->AddObject(AnsiString(TabDepartements[i].szNomDep) + "=" + AnsiString(TabDepartements[i].szCodeDep), (TObject*)TabDepartements[i].iID);
  }
  catch(Exception &e)
  {
    EICDisplayException(TAGL, e);
  }
}

__fastcall TListeDepartements::~TListeDepartements()
{
  try
  {
    delete slDepartements;
    slDepartements = NULL;
  }
  catch(Exception &e)
  {
    EICDisplayException(TAGL, e);
  }
}

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres de sortie :

Valeur retournée :
*******************************************************************************/
STRUCT_DEPARTEMENT __fastcall TListeDepartements::GetTabDepartements(short shItem) const
{
  STRUCT_DEPARTEMENT sDepartement;

  try
  {
    if (shItem == AUCUN_DEP)
    {
      sDepartement.aNomDep  = EmptyStr;
      sDepartement.aCodeDep = EmptyStr;
      sDepartement.iID      = -1;
    }
    else
    {
      sDepartement.aNomDep  = slDepartements->Names[shItem];
      sDepartement.aCodeDep = EXTRAIRE_CH(slDepartements->Strings[shItem], slDepartements->Strings[shItem].Pos('=')+1, slDepartements->Strings[shItem].Length());
      sDepartement.iID      = (int )slDepartements->Objects[shItem];
    }
  }
  catch(Exception &e)
  {
    EICDisplayException(TAGL, e);
  }
  return sDepartement;
}

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres de sortie :

Valeur retournée :
*******************************************************************************/
short __fastcall TListeDepartements::GetCount() const
{
  return slDepartements->Count;
}
//----------------------------------------------------------------------------------
// Fonction permettant de récupérer le nom du Département en fonction de son Code
//----------------------------------------------------------------------------------
AnsiString __fastcall TListeDepartements::CodeDepToNomDep(const AnsiString &aCodeDep) const
{
  try
  {
    for(int i=0; i < slDepartements->Count ; i++)
      if(aCodeDep == EXTRAIRE_CH(slDepartements->Strings[i], slDepartements->Strings[i].Pos('=')+1, slDepartements->Strings[i].Length()))
        return slDepartements->Names[i];
  }
  catch(Exception &e)
  {
    EICDisplayException(TAGL, e);
  }
  return EmptyStr;
}
//----------------------------------------------------------------------------------
// Fonction permettant de récupérer le nom du Département en fonction de son Code
//----------------------------------------------------------------------------------
STRUCT_DEPARTEMENT __fastcall TListeDepartements::IDToDepartement(const int iID) const
{
  STRUCT_DEPARTEMENT sDepartement;
  try
  {
    for(int i=0; i < slDepartements->Count ; i++)
    {
      sDepartement.iID = (int)slDepartements->Objects[i];
      if(sDepartement.iID == iID)
      {
        sDepartement.aNomDep  = slDepartements->Names[i];
        sDepartement.aCodeDep = EXTRAIRE_CH(slDepartements->Strings[i], slDepartements->Strings[i].Pos('=')+1, slDepartements->Strings[i].Length());
        return sDepartement;
      }
    }
    memset(&sDepartement, 0, sizeof(sDepartement));
  }
  catch(Exception &e)
  {
    memset(&sDepartement, 0, sizeof(sDepartement));
    EICDisplayException(TAGL, e);
  }
  return sDepartement;
}
